//
//  YYLoginViewController.m
//  youyuanhuaxia
//
//  Created by 李刚 on 16/5/30.
//  Copyright © 2016年 CaoJIGen. All rights reserved.
//

#import "YYLoginViewController.h"
#import "YYRegisterViewController.h"
#import "MBProgressHUD.h"



@interface YYLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passworldTextField;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation YYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //获得nib视图数组
    UIView *nib = [[[NSBundle mainBundle]loadNibNamed:@"YYLoginViewController" owner:self options:nil] lastObject];

    //获得屏幕的Frame
    CGRect tmpFrame = [[UIScreen mainScreen] bounds];
    //设置自定义视图的中点为屏幕的中点
    [nib setCenter:CGPointMake(tmpFrame.size.width, tmpFrame.size.height)];

    [_userNameTextField becomeFirstResponder];
    
    //给文本框添加事件
    [self.userNameTextField addTarget:self action:@selector(changeTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.passworldTextField addTarget:self action:@selector(changeTextField:) forControlEvents:UIControlEventEditingChanged];
    
    //每输入一个字符就变成点 用语密码输入
    self.passworldTextField.secureTextEntry = YES;
    //输入框中是否有个叉号，在什么时候显示，用于一次性删除输入框中的内容
    self.userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passworldTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    [self initUser];
}

//--------------------初始化用户名和密码--------------------
- (void)initUser{
    
    NSDictionary *people = @{
                             @"name" : @"123",
                             @"password" : @"123"
                             
                             };
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"user.plist"];
    
    [people writeToFile:filePath atomically:YES];
    
    NSLog(@"%@",filePath);
    
}

/**如果姓名和密码文本框都不为空时,让登陆按钮能点击*/
- (void)changeTextField:(UITextField *)textField {
    
    if (_userNameTextField.text.length > 0 && _passworldTextField.text.length > 0) {
        _loginBtn.enabled = YES;
    }else{
        _loginBtn.enabled = NO;
    }
}
//获取路径的方法
- (NSString *)filePathWithFileName :(NSString *)fileName{
    
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:fileName];
    
}

//点击注册按钮
- (IBAction)registerBtnClick:(UIButton *)sender {
    
    NSLog(@"registerBtnClick");
    
    YYRegisterViewController *registerVC = [[YYRegisterViewController alloc] init];

//    [self.navigationController pushViewController:registerVC animated:YES];
    [self presentViewController:registerVC animated:YES completion:nil];
    
}
//点击登陆按钮
- (IBAction)loginBtnClick:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:self.view];
    
    [self.view addSubview:hud];
    
    hud.dimBackground = YES;//灰色效果
    hud.labelText = @"正在登陆";
    [hud show:YES];//显示出来
    
    /** 获取 用户名 和 密码 */
    NSString *filePath = [self filePathWithFileName:@"user.plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    NSLog(@"%@",[dict objectForKey:@"name"]);
    NSLog(@"%@",[dict objectForKey:@"password"] );
    
    
    //延时登陆
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if ([self.userNameTextField.text isEqualToString: [dict objectForKey:@"name"] ] && [self.passworldTextField.text isEqualToString:[dict objectForKey:@"password"]]) {
            
            //登陆成功
            NSLog(@"登陆成功。。。");
            hud.dimBackground = YES;//灰色效果
            hud.labelText = @"登陆成功";
            [hud show:YES];//显示出来
            
        }else {
            
            hud.mode = MBProgressHUDModeText;//只显示文字
            hud.dimBackground = NO;
            hud.labelText = @"用户名或密码错误!";
            
            [hud hide:YES afterDelay:1];
            
            [_passworldTextField becomeFirstResponder];
            
            NSLog(@"用户名或密码错误!");
        }
    });


}



@end
